<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <el-form-item label="生产日期">
          <el-date-picker
            v-model="searchParam.planDateSearch"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
            @change="getPlanDate"
          />
        </el-form-item>
        <el-form-item label="产品">
          <el-input
            v-model="searchParam.materialCode"
            placeholder="请输入产品"
            clearable
            @keyup.enter="search"
            readonly
            @click="showWorkOrderSelect"
          >
            <template #append>
              <el-button @click="resetMaterial"> 清除 </el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="炉号">
          <el-input
            v-model="searchParam.lnum"
            placeholder="请输入炉号"
            clearable
            @keyup.enter="search"
          />
        </el-form-item>
        <!--<el-form-item label="区分">
          <el-select
            v-model="searchParam.dataDis"
            placeholder="请选择区分"
            clearable
          >
            <el-option
              v-for="item in dataDisList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>-->
        <el-form-item label="是否合格">
          <el-select
            v-model="searchParam.isValid"
            placeholder="请选择是否合格"
            clearable
          >
            <el-option
              v-for="item in useDictionary('YesNo')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search"
            ><i-ep-search />搜索</el-button
          >
          <el-button @click="reset"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()"
          ><i-ep-plus />新增</el-button
        >
        <!-- <el-button type="success" plain @click="openDialogDailyRecord()"
          >熔炼日记录</el-button
        > -->
        <el-button
          type="primary"
          plain
          @click="
            useDownload(
              '/mes/mesproducintautodata/export',
              '熔炼记录',
              searchParam
            )
          "
        >
          <template #icon><i-ep-download /></template>导出</el-button
        >
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :max-height="tableHeight"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="index" label="序号" width="55" align="center" />
        <el-table-column
          label="熔炼编号"
          prop="processingOrderNumber"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="产品名称"
          prop="materialName"
          min-width="180"
          show-overflow-tooltip
        >
          <template #default="scope">
            {{ scope.row.materialName }}
          </template>
        </el-table-column>
        <el-table-column
          label="产品图号"
          prop="figureNumber"
          min-width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="生产日期"
          prop="planActualDate"
          min-width="100"
          show-overflow-tooltip
          align="center"
          :formatter="formatDate"
        />
        <el-table-column
          label="炉号"
          prop="lnum"
          min-width="100"
          show-overflow-tooltip
        />
        <!--<el-table-column
          label="批次"
          prop="plannedBatch"
          min-width="100"
          show-overflow-tooltip
        />-->
        <el-table-column
          label="出铁重量"
          prop="materialWeight"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="电量"
          prop="electricQuantity"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="司炉工"
          prop="fireman"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="是否合格"
          prop="isValid"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            {{ scope.row.isValid === "1" ? "是" : "否" }}
          </template>
        </el-table-column>
        <el-table-column
          label="熔炼开始时间"
          prop="beginTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="熔炼结束时间"
          prop="endTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="精炼开始时间"
          prop="refineBeginTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="精炼结束时间"
          prop="refineEndTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <!--<el-table-column
          label="区分"
          prop="dataDis"
          min-width="100"
          show-overflow-tooltip
          align="center"
        >
          <template #default="scope">
            <span v-if="scope.row.dataDis === '1'">包</span>
            <span v-if="scope.row.dataDis === '2'">模</span>
          </template>
        </el-table-column>-->
        <!--<el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        />-->
        <el-table-column
          label="创建时间"
          prop="createTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="更新时间"
          prop="lastUpdateTime"
          min-width="160"
          show-overflow-tooltip
          align="center"
        />
        <el-table-column
          label="更新人"
          prop="lastUpdatedUserName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column fixed="right" label="操作" width="300" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetailNew(scope.row)"
              ><i-ep-search />
              查看出铁水
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <molten-iron-edit
      :visible="addDialogNew.visible"
      :title="addDialogNew.title"
      :data="addDialogNew.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialogNew.readvisible"
      @change-visible="changeVisibleNew"
    />
    <mesproducintautodata-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      :dataType="addDialog.dataType"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
    <conten-select
      :visible="selectDialogSELECT.visible"
      @change-visible="changeVisibleSELECT"
      :plantId="searchParam.plantId"
      :brekList="brekList"
    />
    <dailyRecordFilling-dialog
      :visible="addDialogDailyRecord.visible"
      :title="addDialogDailyRecord.title"
      @change-visible="changeVisibleDailyRecord"
    />
  </div>
</template>

<script setup lang="ts">
import {useTable, useDictionary, useComputeTableHeight, useDownload} from "@/utils/useTable";
import request from "@/utils/request";

defineOptions({
  name: "MESPLANEXECUTERECORDSMELTRECORD",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/mes/mesproducintautodata/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  planId: "",
  lnum: "",
  materialName: "",
  dataDis: "",
  dataType: "1",
  isValid: "",
  planDateSearch: [],
  planDateStart: "",
  planDateEnd: "",
};

const deleteParam = {
  api: "/mes/mesproducintautodata/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

const tableHeight = ref<any>("auto");
onMounted(() => {
  getTableList();
  setTimeout(() => {
    tableHeight.value = useComputeTableHeight();
  }, 100);
});

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
  dataType: "1",
});
const addDialogNew = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});
// 查询详情
async function getInfo(id: string) {
  request({
    url: `/mes/mesproducintautodata/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开自动化生产数据表表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改熔炼记录";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增熔炼记录";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开自动化生产数据表表单弹窗 详情 */
function openDialogdetailNew(roleObj?: any) {
  addDialogNew.title = "查看出铁水";
  addDialogNew.data = roleObj;
  addDialogNew.visible = true;
  addDialogNew.readvisible = true;
}
function openDialogdetail(roleObj?: any) {
  addDialog.title = "查看熔炼记录";
  addDialog.data = roleObj;
  addDialog.visible = true;
  addDialog.readvisible = true;
}
function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
function changeVisibleNew(value: boolean) {
  addDialogNew.visible = value;
  if (!value) {
    addDialogNew.data = {};
  }
}
function getPlanDate(data: any) {
  if (data) {
    searchParam.value.planDateStart = data[0];
    searchParam.value.planDateEnd = data[1];
  } else {
    searchParam.value.planDateStart = "";
    searchParam.value.planDateEnd = "";
  }
}

//时间格式处理，yyyy-mm-dd
function formatDate(row, column) {
  const date = row[column.property];
  if (!date) return "";
  return date.split(" ")[0]; // 返回日期的年月日部分，而隐藏时间部分
}
const showWorkOrderSelect = () => {
  selectDialogSELECT.value.visible = true;
};
//选择物料弹窗
const selectDialogSELECT = ref({
  visible: false,
});
const changeVisibleSELECT = (val: boolean, data: any) => {
  selectDialogSELECT.value.visible = val;
  if (data) {
    searchParam.value.materialId = data.id;
    searchParam.value.materialCode = data.materialCode;
  }
};
// 回显的数据
const brekList = ref([]);
const resetMaterial = () => {
  searchParam.value.materialId = "";
  searchParam.value.materialCode = "";
};
const dataDisList = [
  { value: "1", label: "包" },
  { value: "2", label: "模" },
];
async function deleteItem(id: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const res = await request({
      url: `/mes/mesproducintautodata/deleteItem/${id}`,
      method: "post",
    });
    if (res.code === 200) {
      ElMessage.success("删除成功");
    }
    search();
  });
}

// 熔炼计划

const addDialogDailyRecord = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
  dataType: "1",
});
/** 打开自动化生产数据表表单弹窗 */
function openDialogDailyRecord(roleObj?: Object) {
  addDialogDailyRecord.title = "熔炼日记录";
  addDialogDailyRecord.visible = true;

  addDialogDailyRecord.readvisible = false;
}
function changeVisibleDailyRecord(value: boolean) {
  addDialogDailyRecord.visible = value;
}
</script>
